﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using BSU.Math.Geometry;

namespace BSU.GraduateWork.Data.Model
{
    [Serializable]
    [Table(Name = "Solution")]
    public class Solution:IEntity<int>
    {
        [Column(Name = "id", DbType = "Int IDENTITY", IsPrimaryKey = true, IsDbGenerated = true,
            CanBeNull = false, AutoSync = AutoSync.OnInsert, UpdateCheck = UpdateCheck.Never)]
        public int Id { get; set; }

        private EntitySet<Point> _Points = new EntitySet<Point>();

        //[Association(Storage = "_Points", OtherKey = "SolutionId")]
        public EntitySet<Point> Points
        {
            get { return _Points; }
            set { _Points.Assign(value); }
        }

        private IList<Point4D> _u;

        public IList<Point4D> U 
        {
            get
            {
                //_u = new List<Point4D>();

                //foreach (var point in Points)
                //{
                //    _u.Add(new Point4D(point.X, point.Y, point.Z, point.U, point.IsBoundary));
                //}

                return _u;

                //return (
                //    from point 
                //    in Points 
                //    select new Point4D(point.X, point.Y, point.Z, point.U, point.IsBoundary))
                //        .ToList();
            }
            set
            {
                _u = new List<Point4D>(value);
                
                Points = new EntitySet<Point>();

                foreach (var point in _u)
                {
                    Points.Add(new Point
                    {
                        X = point.X,
                        Y = point.Y,
                        Z = point.Z,
                        U = point.U,
                        IsBoundary = point.IsBoundary
                    });
                }

                
                //Points.Assign(
                //    (from point 
                //    in value
                //    select new Point{
                //        X = point.X, 
                //        Y = point.Y, 
                //        Z = point.Z, 
                //        U = point.U, 
                //        IsBoundary = point.IsBoundary}
                //        ).ToList());
            }
        }

        [Column(Name = "IterationsCount", DbType = "int", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
        public int IterationsCount { get; set; }

        [Column(Name = "ExecutionTime", DbType = "time(7)", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
        public TimeSpan ExecutionTime { get; set; }

        public Solution()
        {
            Id = -1;
        }
    }
}
